<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="患者姓名" prop="patientName">
      <el-input v-model="dataForm.patientName" placeholder="患者姓名"></el-input>
    </el-form-item>
    <el-form-item label="患者年龄" prop="patientAge">
      <el-input v-model="dataForm.patientAge" placeholder="患者年龄"></el-input>
    </el-form-item>
    <el-form-item label="患者身高" prop="patientHeight">
      <el-input v-model="dataForm.patientHeight" placeholder="患者身高"></el-input>
    </el-form-item>
    <el-form-item label="患者体重" prop="patientWeight">
      <el-input v-model="dataForm.patientWeight" placeholder="患者体重"></el-input>
    </el-form-item>
    <el-form-item label="婚姻状况 0未婚 1已婚 " prop="patientMarrystate">
      <el-input v-model="dataForm.patientMarrystate" placeholder="婚姻状况 0未婚 1已婚 "></el-input>
    </el-form-item>
    <el-form-item label="生育状况0未生育 1已生育  2其它" prop="patientHaschild">
      <el-input v-model="dataForm.patientHaschild" placeholder="生育状况0未生育 1已生育  2其它"></el-input>
    </el-form-item>
    <el-form-item label="疾病史" prop="patientDiseasesHistory">
      <el-input v-model="dataForm.patientDiseasesHistory" placeholder="疾病史"></el-input>
    </el-form-item>
    <el-form-item label="患者金额" prop="patientMoney">
      <el-input v-model="dataForm.patientMoney" placeholder="患者金额"></el-input>
    </el-form-item>
    <el-form-item label="患者密码" prop="patientPwd">
      <el-input v-model="dataForm.patientPwd" placeholder="患者密码"></el-input>
    </el-form-item>
    <el-form-item label="患者身份证" prop="patientIdCard">
      <el-input v-model="dataForm.patientIdCard" placeholder="患者身份证"></el-input>
    </el-form-item>
    <el-form-item label="患者手机号" prop="patientPhone">
      <el-input v-model="dataForm.patientPhone" placeholder="患者手机号"></el-input>
    </el-form-item>
    <el-form-item label="患者性别 0:男 1女" prop="patientSex">
      <el-input v-model="dataForm.patientSex" placeholder="患者性别 0:男 1女"></el-input>
    </el-form-item>
    <el-form-item label="患者出生日期" prop="patientBirthday">
      <el-input v-model="dataForm.patientBirthday" placeholder="患者出生日期"></el-input>
    </el-form-item>
    <el-form-item label="支付金额" prop="payMoney">
      <el-input v-model="dataForm.payMoney" placeholder="支付金额"></el-input>
    </el-form-item>
    <el-form-item label="问诊次数" prop="patientAskTime">
      <el-input v-model="dataForm.patientAskTime" placeholder="问诊次数"></el-input>
    </el-form-item>
    <el-form-item label="患者头像" prop="patientImg">
      <el-input v-model="dataForm.patientImg" placeholder="患者头像"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          patientName: '',
          patientAge: '',
          patientHeight: '',
          patientWeight: '',
          patientMarrystate: '',
          patientHaschild: '',
          patientDiseasesHistory: '',
          patientMoney: '',
          patientPwd: '',
          patientIdCard: '',
          patientPhone: '',
          patientSex: '',
          patientBirthday: '',
          payMoney: '',
          patientAskTime: '',
          patientImg: ''
        },
        dataRule: {
          patientName: [
            { required: true, message: '患者姓名不能为空', trigger: 'blur' }
          ],
          patientAge: [
            { required: true, message: '患者年龄不能为空', trigger: 'blur' }
          ],
          patientHeight: [
            { required: true, message: '患者身高不能为空', trigger: 'blur' }
          ],
          patientWeight: [
            { required: true, message: '患者体重不能为空', trigger: 'blur' }
          ],
          patientMarrystate: [
            { required: true, message: '婚姻状况 0未婚 1已婚 不能为空', trigger: 'blur' }
          ],
          patientHaschild: [
            { required: true, message: '生育状况0未生育 1已生育  2其它不能为空', trigger: 'blur' }
          ],
          patientDiseasesHistory: [
            { required: true, message: '疾病史不能为空', trigger: 'blur' }
          ],
          patientMoney: [
            { required: true, message: '患者金额不能为空', trigger: 'blur' }
          ],
          patientPwd: [
            { required: true, message: '患者密码不能为空', trigger: 'blur' }
          ],
          patientIdCard: [
            { required: true, message: '患者身份证不能为空', trigger: 'blur' }
          ],
          patientPhone: [
            { required: true, message: '患者手机号不能为空', trigger: 'blur' }
          ],
          patientSex: [
            { required: true, message: '患者性别 0:男 1女不能为空', trigger: 'blur' }
          ],
          patientBirthday: [
            { required: true, message: '患者出生日期不能为空', trigger: 'blur' }
          ],
          payMoney: [
            { required: true, message: '支付金额不能为空', trigger: 'blur' }
          ],
          patientAskTime: [
            { required: true, message: '问诊次数不能为空', trigger: 'blur' }
          ],
          patientImg: [
            { required: true, message: '患者头像不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/patient/patient/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.patientName = data.patient.patientName
                this.dataForm.patientAge = data.patient.patientAge
                this.dataForm.patientHeight = data.patient.patientHeight
                this.dataForm.patientWeight = data.patient.patientWeight
                this.dataForm.patientMarrystate = data.patient.patientMarrystate
                this.dataForm.patientHaschild = data.patient.patientHaschild
                this.dataForm.patientDiseasesHistory = data.patient.patientDiseasesHistory
                this.dataForm.patientMoney = data.patient.patientMoney
                this.dataForm.patientPwd = data.patient.patientPwd
                this.dataForm.patientIdCard = data.patient.patientIdCard
                this.dataForm.patientPhone = data.patient.patientPhone
                this.dataForm.patientSex = data.patient.patientSex
                this.dataForm.patientBirthday = data.patient.patientBirthday
                this.dataForm.payMoney = data.patient.payMoney
                this.dataForm.patientAskTime = data.patient.patientAskTime
                this.dataForm.patientImg = data.patient.patientImg
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/patient/patient/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'patientName': this.dataForm.patientName,
                'patientAge': this.dataForm.patientAge,
                'patientHeight': this.dataForm.patientHeight,
                'patientWeight': this.dataForm.patientWeight,
                'patientMarrystate': this.dataForm.patientMarrystate,
                'patientHaschild': this.dataForm.patientHaschild,
                'patientDiseasesHistory': this.dataForm.patientDiseasesHistory,
                'patientMoney': this.dataForm.patientMoney,
                'patientPwd': this.dataForm.patientPwd,
                'patientIdCard': this.dataForm.patientIdCard,
                'patientPhone': this.dataForm.patientPhone,
                'patientSex': this.dataForm.patientSex,
                'patientBirthday': this.dataForm.patientBirthday,
                'payMoney': this.dataForm.payMoney,
                'patientAskTime': this.dataForm.patientAskTime,
                'patientImg': this.dataForm.patientImg
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
